#include "../../framework/framework.h"
#include "../inc/xSprite.h"
#include "../inc/xPict.h"
#include "../inc/xFont.h"
#include "../../framework/framework.h"
namespace Sky
{

	xSprite::xSprite():m_pFont(vNull),m_pPict(vNull),m_bBegined(vFalse)
	{
		
	}
	xSprite::~xSprite()
	{
		SAFE_RELEASE(m_Res);
	}
	TResult xSprite::onCreate( xDevice* device )
	{
		if(m_Res != vNull)
		{
			onDelete(device);
		}
		myDxV_Ret_hr_M(D3DXCreateSprite( device->getDevice9(), &m_Res ),XE_APICALL);
		return XE_OK;
	}

	TResult xSprite::onCreate( xDxApp* app )
	{
		Initialise();
		return XE_OK;
	}
	TResult xSprite::onDelete(xDevice* device)
	{
		SAFE_RELEASE(m_Res);
		return XE_OK;
	}

	TResult xSprite::onReset( xDevice* device )
	{
		xAssertA(m_Res&&_XT("m_Res == vNull in onReset()"));
		myDxV_Ret_hr_M(m_Res->OnResetDevice(),XE_APICALL);
		return XE_OK;
	}

	TResult xSprite::onLost(xDevice* device)
	{
		 xAssertA(m_Res&&_XT("m_Res == vNull in onLost()"));
		myDxV_Ret_hr_M(m_Res->OnLostDevice(),XE_APICALL);
		return XE_OK;
	}

	TResult xSprite::begin( TUint D3DXSPRITE_ /*= D3DXSPRITE_ALPHABLEND*/ )
	{
		if(m_Res == vNull)
		{
			myDeV_Ret_hr_M(trySelfCreate(),XE_APICALL);
		}
		myDxV_Ret_hr_M(m_Res->Begin(D3DXSPRITE_),XE_APICALL);
		m_Res->SetTransform(&(m_Transform*m_Origion));
		m_bBegined = vTrue;
		return XE_OK;
	}

	TVoid xSprite::end()
	{
		if(m_bBegined)
		{
			m_Res->End();
			m_bBegined = vFalse;
		}
	}

	TResult xSprite::draw( xPict* pict,TRect* src,xVector3* dst,xVector3* center,xColor color )
	{
		RECT rect;
		RECT *pRect = vNull;
		if(src != vNull)
		{
			rect.left = src->x;
			rect.top = src->y;
			rect.right = src->x+src->w;
			rect.bottom = src->y+src->h;
			pRect = &rect;
		};
		xAssertA(pict&&_XT("pict == vNull in draw()"));
		m_Res->SetTransform(&(m_Transform*m_Origion));
		m_Res->Draw(pict->getNormalTexture(),pRect,center,dst,color);
		return XE_OK;
	}

	TResult xSprite::draw( xPict* pict,TRect* src /*= vNull*/,xVector5* dst /*= vNull*/,xVector3* center /*= vNull*/,xColor color /*= D3DCOLOR_RGBA(0xff,0xff,0xff,0xff)*/ )
	{
		RECT rect;
		HRESULT hr;
		RECT *pRect = vNull;
		D3DXMATRIXA16 matScale,matTrans;
		D3DXMatrixIdentity(&matScale);
		D3DXMatrixIdentity(&matTrans);
		if(src != vNull)
		{
			rect.left = src->x;
			rect.top = src->y;
			rect.right = src->x+src->w;
			rect.bottom = src->y+src->h;
			pRect = &rect;

			float fScaleW = (float)dst->w/src->w;
			float fScaleH = (float)dst->h/src->h;
			D3DXMatrixScaling(&matScale,fScaleW,fScaleH,0.0f);
		};
		D3DXMatrixTranslation(&matTrans,dst->x,dst->y,dst->z);
		 xAssertA(pict&&"pict == vNull in draw()");
		hr = m_Res->SetTransform(&(matScale*matTrans*m_Transform*m_Origion));
		hr = m_Res->Draw(pict->getNormalTexture(),pRect,center,vNull,color);
		hr = m_Res->SetTransform(&(m_Transform*m_Origion));
		return XE_OK;
	}

	TResult xSprite::draw(TRect* src,xVector3* dst,xVector3* center,xColor color)
	{
		RECT rect;
		RECT *pRect = vNull;
		if(src != vNull)
		{
			rect.left = src->x;
			rect.top = src->y;
			rect.right = src->x+src->w;
			rect.bottom = src->y+src->h;
			pRect = &rect;
		};
		 xAssertA(m_pPict&&"m_pPict == vNull in draw()");
		//m_Res->SetTransform(&(m_Transform*m_Origion));
		m_Res->Draw(m_pPict->getNormalTexture(),pRect,center,dst,color);
		return XE_OK;
	}
				
	TResult xSprite::draw(const TChar* str,TRect* dst,TInt size,TUint DT_ ,xColor color )
	{
		RECT rect = {dst->x,dst->y,dst->x+dst->w,dst->y+dst->h};
		 xAssertA(m_pFont&&"m_pFont == vNull in draw()");
		//m_Res->SetTransform(&(m_Transform*m_Origion));
		m_pFont->getFont()->DrawText(m_Res,str,size,&rect,DT_,color);
		return XE_OK;
	}


	TResult xSprite::draw(xFont* font,TChar* str,TRect* dst,TInt size,TUint DT_,xColor color)
	{
		RECT rect = {dst->x,dst->y,dst->x+dst->w,dst->y+dst->h};
		 xAssertA(font&&_XT("font == vNull in draw()"));
		//m_Res->SetTransform(&(m_Transform*m_Origion));
		font->getFont()->DrawText(m_Res,str,size,&rect,DT_,color);
		return XE_OK;
	}

	TResult xSprite::attachFont( xFont* font )
	{
		 xAssertA(font&&"font == vNull in attachPict()");
		if(font->getFont() == vNull){
			myDeV_Ret_M(font->trySelfCreate())
		}
		m_pFont = font;
		return XE_OK;
	}

	TResult xSprite::attachPict( xPict* pict )
	{
		 xAssertA(pict&&m_pPict->isNormalTexture());
		if(m_pPict->getNormalTexture() == vNull){
			myDeV_Ret_M(pict->trySelfCreate());
		}
		m_pPict = pict;
		return XE_OK;
	}

	xSprite& xSprite::setMatrix( xMatrix* matrix )
	{
		if(matrix == vNull)
		{
			D3DXMatrixIdentity(&m_Transform);
		}
		else
		{
			m_Transform = *matrix;
		}
		return *this;
	}

	xSprite& xSprite::setCenter( TInt x /*= 0*/,TInt y /*= 0*/,TInt z /*= 0*/ )
	{
		m_CenterX = x;
		m_CenterY = y;
		m_CenterZ = z;
		D3DXMatrixTranslation(&m_Origion,m_CenterX,m_CenterY,m_CenterZ);
		return *this;
	}

	TVoid xSprite::Initialise()
	{
		D3DXMatrixIdentity(&m_Origion);
		D3DXMatrixIdentity(&m_Transform);
		m_CenterY = 0;
		m_CenterX = 0;
		m_CenterZ = 0;
	}

	TVoid xSprite::Finalise()
	{
		SAFE_RELEASE(m_Res);
	}

	Sky::NType::TSize xSprite::calTextSize( xFont* font,TChar* str,TRect* dst )
	{
		RECT rect;
		rect.left =  dst->x;
		rect.top =  dst->y;
		rect.right = dst->x + dst->w;
		rect.bottom = dst->y + dst->h;
		TInt h = font->getFont()->DrawText(m_Res,str,-1,&rect,DT_SINGLELINE | DT_CALCRECT,D3DCOLOR_RGBA(0,0,0,0));
		return TSize(rect.right - rect.left,h);
	}

	//TSize xSprite::calTextSize( xFont* font,TChar* str,TRect* dst )
	//{
	//	RECT rect;
	//	rect.left =  dst->x;
	//	rect.top =  dst->y;
	//	rect.right = dst->x + dst->w;
	//	rect.bottom = dst->y + dst->h;
	//	TInt h = font->getFont()->DrawText(m_Res,str,-1,&rect,DT_SINGLELINE | DT_CALCRECT,D3DCOLOR_RGBA(0,0,0,0));
	//	return TSize(rect.right - rect.left,h);
	//}
}